package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.AppUpgradeBo;
import com.joker.orange.domain.vo.AppUpgradeVo;
import com.joker.orange.domain.AppUpgrade;
import com.joker.orange.mapper.AppUpgradeMapper;
import com.joker.orange.service.IAppUpgradeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 升级配置Service业务层处理
 *
 * @author Joker
 * @date 2024-07-08
 */
@RequiredArgsConstructor
@Service
public class AppUpgradeServiceImpl implements IAppUpgradeService {

    private final AppUpgradeMapper baseMapper;

    /**
     * 查询升级配置
     */
    @Override
    public AppUpgradeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询升级配置列表
     */
    @Override
    public TableDataInfo<AppUpgradeVo> queryPageList(AppUpgradeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUpgrade> lqw = buildQueryWrapper(bo);
        Page<AppUpgradeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询升级配置列表
     */
    @Override
    public List<AppUpgradeVo> queryList(AppUpgradeBo bo) {
        LambdaQueryWrapper<AppUpgrade> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUpgrade> buildQueryWrapper(AppUpgradeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUpgrade> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getVersionNo()), AppUpgrade::getVersionNo, bo.getVersionNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDownloadUrl()), AppUpgrade::getDownloadUrl, bo.getDownloadUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getUpgradeIntroduction()), AppUpgrade::getUpgradeIntroduction, bo.getUpgradeIntroduction());
        lqw.eq(StringUtils.isNotBlank(bo.getForceUpdate()), AppUpgrade::getForceUpdate, bo.getForceUpdate());
        return lqw;
    }

    /**
     * 新增升级配置
     */
    @Override
    public Boolean insertByBo(AppUpgradeBo bo) {
        AppUpgrade add = BeanUtil.toBean(bo, AppUpgrade.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改升级配置
     */
    @Override
    public Boolean updateByBo(AppUpgradeBo bo) {
        AppUpgrade update = BeanUtil.toBean(bo, AppUpgrade.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUpgrade entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除升级配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
